package ljl.codetop;

public class _152_max_product {

    /**
     * 不对，try 不出来
     */
    static class have_a_try {
        // 我记着做过，记录最大值和最小值，如果是负数就和最小值乘，正数就和最大值乘，我们开始吧！
        public int maxProduct(int[] nums) {
            int[] max = new int[nums.length];
            int[] min = new int[nums.length];
            max[0] = min[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                if (nums[i] < 0) {
                    max[i] = min[i - 1] * nums[i];
                } else {
                    max[i] = max[i - 1] * nums[i];
                }
            }
            int res = Integer.MIN_VALUE;
            for (int i : max) {
                if (res < i) res = i;
            }
            return res;
        }
    }

    static class off1 {

        public int maxProduct(int[] nums) {
            int length = nums.length;
            int[] maxF = nums.clone();
            int[] minF = nums.clone();
            for (int i = 1; i < length; ++i) {
                maxF[i] = Math.max(maxF[i - 1] * nums[i], Math.max(nums[i], minF[i - 1] * nums[i]));
                minF[i] = Math.min(minF[i - 1] * nums[i], Math.min(nums[i], maxF[i - 1] * nums[i]));
            }
            int ans = maxF[0];
            for (int i = 1; i < length; ++i) {
                ans = Math.max(ans, maxF[i]);
            }
            return ans;
        }
    }

    static class off2 {
        public int maxProduct(int[] nums) {
            int max = nums[0], min = nums[0], ans = nums[0];
            int length = nums.length;
            for (int i = 1; i < length; ++i) {
                int maxOld = max;
                max = Math.max(max * nums[i], Math.max(nums[i], min * nums[i]));
                min = Math.min(min * nums[i], Math.min(nums[i], maxOld * nums[i]));
                ans = Math.max(max, ans);
            }
            return ans;
        }
    }
}
